#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
#include<vector>
#include<string>



#if 0
int main() {
	int n;
	cin >> n;
	vector<int> arr(n * 3);
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	sort(arr.begin(), arr.end());
	int index = arr.size() - 2;
	int ret = 0;
	while (n--) {
		ret += arr[index];
		index -= 2;
	}
	cout << ret << endl;
	return 0;
}
int main() {
	string str;
	getline(cin, str);
	reverse(str.begin(), str.end());
	string::iterator begin = str.begin();
	string::iterator end = str.begin();
	while (end != str.end() ) {
		while (end != str.end() && *end != ' ') {
			end++;
		}
		reverse(begin, end);
		if (end == str.end()) break;
		end++;
		begin = end;
	}
	//reverse(begin, end);

	cout << str << endl;
	system("pause");
	return 0;
}
#endif




vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
	if (intervals.size() == 0) {
		intervals.push_back(newInterval);
		return intervals;
	}
	int i = 0;
	int left = 0, right = 0;
	while (i < intervals.size()) {
		if (newInterval[0] < intervals[i][0]) {
			left = newInterval[0];
			break;
		}
		if (newInterval[0] >= intervals[i][0] && newInterval[0] <= intervals[i][1]) {
			left = intervals[i][0];
			break;
		}
		i++;
	}
	while (i < intervals.size() ) {
		if (newInterval[1] < intervals[i][0]) {
			right = newInterval[1];
			intervals.insert(intervals.begin() + i, { left, right });
			break;
		}
		if (newInterval[1] >= intervals[i][0] && newInterval[1] <= intervals[i][1]) {
			right = intervals[i][1];
			intervals.erase(intervals.begin() + i);
			intervals.insert(intervals.begin() + i, { left, right });
			break;
		}
		intervals.erase(intervals.begin() + i);
		if (i == intervals.size() - 1) {
			intervals.insert(intervals.begin() + i, { left, newInterval[1] });
		}
	}
	return intervals;
}

int main() {
	vector<vector<int >> intervals;
	intervals.push_back({ 1,5 });
	vector<int> newInterval({2,7});
	insert( intervals, newInterval);
}